package com.hanscan.persistence.entities;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;


@Entity
@Table(name = "events")
@SequenceGenerator(name = "eventGen", sequenceName = "eventGen",
allocationSize = 1)
@NamedQueries(
{
    @NamedQuery(name = "getAllEvents",
    query = "SELECT g FROM Event g"),
    @NamedQuery(name = "findEventById",
    query = "SELECT g FROM Event g " + "WHERE g.eventId=?1"),
    @NamedQuery(name = "findEventByUser",
    query = "SELECT g FROM Event g "
    + "WHERE g.user.userId=?1 ORDER BY g.dateTime"),
    @NamedQuery(name = "findEventsByUserBetweenDays",
    query = "SELECT g FROM Event g "
    + "WHERE (g.dateTime >= ?1 AND g.dateTime <= ?2) AND g.user.userId=?3 "
    + "ORDER BY g.dateTime"),
    @NamedQuery(name = "findEventsByTerminalBetweenDays",
    query = "SELECT g FROM Event g "
    + "WHERE (g.dateTime >= ?1 AND g.dateTime <= ?2) AND UPPER(g.terminal.mac)=?3 "
    + "ORDER BY g.dateTime"),
    @NamedQuery(name = "findTodayEvents",
    query = "SELECT g FROM Event g "
    + "WHERE  (g.dateTime >= ?1 AND g.dateTime <= ?2) " + "ORDER BY g.dateTime"),
    @NamedQuery(name = "findEventsByUserIdAndTime",
    query =
    "SELECT e FROM Event e "
    + "WHERE e.user.userId=?1 and (e.dateTime>= ?2 and e.dateTime<= ?3) and (e.eventType.idEventType =?4 or e.eventType.idEventType =?5 or e.eventType.idEventType =?6)"),
    @NamedQuery(name = "findEventsByTime",
    query =
    "SELECT e FROM Event e " + "WHERE (e.dateTime>= ?1 and e.dateTime<= ?2)"),
    @NamedQuery(name = "findEventsByUserId2",
    query =
    "SELECT e FROM Event e " + "WHERE e.user.userId=?1"),
    @NamedQuery(name = "findEventsByTimeAndEventTypeAndUserAndTerminal",
    query =
    "SELECT e FROM Event e " + "WHERE (e.dateTime>= ?1 and e.dateTime<= ?2) "
    + "and e.eventType.idEventType= ?3 and UPPER(e.user.nsp)=?4 "
    + "and UPPER(e.terminal.mac)=?5"),
    @NamedQuery(name = "findEventsByTimeAndEventTypeAndUser",
    query =
    "SELECT e FROM Event e " + "WHERE (e.dateTime>= ?1 and e.dateTime<= ?2) "
    + "and e.eventType.idEventType= ?3 and UPPER(e.user.nsp)= ?4 "),   
    @NamedQuery(name = "findEventsByTimeAndEventTypeAndUserId",
    query =
    "SELECT e FROM Event e " + "WHERE (e.dateTime>= ?1 and e.dateTime<= ?2) "
    + "and e.eventType.idEventType= ?3 and e.user.userId= ?4 "),
    @NamedQuery(name = "findEventsByTimeAndEventTypeAndTerminal",
    query =
    "SELECT e FROM Event e " + "WHERE (e.dateTime>= ?1 and e.dateTime<= ?2) "
    + "and e.eventType.idEventType= ?3 and UPPER(e.terminal.mac)= ?4"),
    @NamedQuery(name = "findEventsByTimeAndEventType",
    query =
    "SELECT e FROM Event e " + "WHERE (e.dateTime>= ?1 and e.dateTime<= ?2) "
    + "and e.eventType.idEventType= ?3"),
    @NamedQuery(name = "findEventsByTimeAndUserAndTerminal",
    query =
    "SELECT e FROM Event e " + "WHERE (e.dateTime>= ?1 and e.dateTime<= ?2) "
    + "and UPPER(e.user.nsp)= ?3 and UPPER(e.terminal.mac)= ?4"),
    @NamedQuery(name = "findEventsByTimeAndUser",
    query =
    "SELECT e FROM Event e " + "WHERE (e.dateTime>= ?1 and e.dateTime<= ?2) "
    + "and UPPER(e.user.nsp)= ?3 "),
    @NamedQuery(name = "findEventsByTimeAndTerminal",
    query =
    "SELECT e FROM Event e " + "WHERE (e.dateTime>= ?1 and e.dateTime<= ?2) "
    + "and UPPER(e.terminal.mac)= ?3 "),
    @NamedQuery(name = "findEventsByEventTypeAndUser",
    query =
    "SELECT e FROM Event e " + "WHERE e.eventType.idEventType= ?1"
    + " and UPPER(e.user.nsp)= ?2 "),
    @NamedQuery(name = "findEventsByEventTypeAndTerminal",
    query =
    "SELECT e FROM Event e " + "WHERE e.eventType.idEventType= ?1"
    + " and UPPER(e.terminal.mac)= ?2"),
    @NamedQuery(name = "findEventsByEventTypeAndUserAndTerminal",
    query =
    "SELECT e FROM Event e " + "WHERE e.eventType.idEventType= ?1"
    + " and UPPER(e.user.nsp)= ?2 and UPPER(e.terminal.mac)= ?3"),
    @NamedQuery(name = "findEventsByEventType",
    query =
    "SELECT e FROM Event e " + "WHERE e.eventType.idEventType= ?1"),
    @NamedQuery(name = "findEventsByUserAndTerminal",
    query =
    "SELECT e FROM Event e " + "WHERE UPPER(e.user.nsp)= ?1 "
    + "and UPPER(e.terminal.mac)= ?2"),
    @NamedQuery(name = "findEventsByUser",
    query =
    "SELECT e FROM Event e " + "WHERE UPPER(e.user.nsp)= ?1 "),
    @NamedQuery(name = "findEventsByTerminal",
    query =
    "SELECT e FROM Event e " + "WHERE UPPER(e.terminal.mac)= ?1 ORDER BY e.dateTime")
})
public class Event implements Serializable
{

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "eventGen")
    @Column(nullable = false)
    private Long eventId;
    //Atributos
    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateTime;
 
    //Relaciones
    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    private Terminal terminal;
    @ManyToOne(fetch = FetchType.EAGER)
    private User user;
    @ManyToOne
    private EventType eventType;

    public Event()
    {
    }

    public Event(Long eventId)
    {
        this.eventId = eventId;
    }

    public Event(Event event)
    {
        this.eventId = event.eventId;
        this.dateTime = event.dateTime;
        this.user = event.user;
        this.terminal = event.terminal;
        this.eventType = event.eventType;
    }

    public Event(Long eventId,
            Date dateTime,
            User user)
    {
        this.eventId = eventId;
        this.dateTime = dateTime;
        this.user = user;
    }

    public Date getDateTime()
    {
        return dateTime;
    }

    public void setDateTime(Date dateTime)
    {
        this.dateTime = dateTime;
    }

    public Long getEventId()
    {
        return eventId;
    }

    public void setEventId(Long eventId)
    {
        this.eventId = eventId;
    }

    public User getUser()
    {
        return user;
    }

    public void setUser(User user)
    {
        this.user = user;
    }

    public EventType getEventType()
    {
        return eventType;
    }

    public void setEventType(EventType eventType)
    {
        this.eventType = eventType;
    }

    public Terminal getTerminal()
    {
        return terminal;
    }

    public void setTerminal(Terminal terminal)
    {
        this.terminal = terminal;
    }

   

}
